<?php

namespace App\Admin\Controllers\Share;

use App\model\broker\BrokerCustomer;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use App\User;
use App\model\broker\BrokerProject;
use App\Repository\ShareRepository;

class BrokerCustomerController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('分销状态')
            ->description('分销状态')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('分销状态')
            ->description('分销状态')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑分销状态')
            ->description('编辑分销状态')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新建分销状态')
            ->description('新建分销状态')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new BrokerCustomer);

        $grid->id('Id');
        $grid->first_id('直推用户')->display(function ($value) {
            return User::find($value)->name ?? '';
        });
        $grid->second_id('分推用户')->display(function ($value) {
            return User::find($value)->name ?? '';
        });
        $grid->user_id('关联用户')->display(function ($value) {
            return User::find($value)->name ?? '';
        });
        $grid->project_id('推广项目')->display(function ($value) {
            return BrokerProject::find($value)->title ?? '';
        });
        //$grid->created_at('创建时间');
        $grid->updated_at('更新时间');
        $grid->status('推广状态')->editable('select',BrokerCustomer::$statusMap);
        $grid->effective_time('切换有效的时间');
        $grid->follow_time('继续跟进中的时间');
        $grid->transaction_time('成交时间');
        $grid->effective_first_reward('有效推广奖励');
        $grid->effective_share_reward('有效推广朋友圈奖励');
        $grid->follow_first_reward('继续跟进推广奖励');
        $grid->follow_share_reward('跟进分享朋友圈奖励');
        $grid->transaction_first_reward('成交推广奖励');
        $grid->transaction_share_reward('成交分享朋友圈奖励');
        $grid->second_reward('直推上级奖励 ');
        $grid->disableCreateButton();
        $grid->disableActions();
        //筛选功能
        $grid->filter(function (Grid\Filter $filter) {
            // 去掉默认的id过滤器
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->equal('status', '进度状态')->select(BrokerCustomer::$statusMap);
                $filter->equal('user_id', '分享用户')->mac(User::getLikeUser(\Request::get('user_id')??''));
            });
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(BrokerCustomer::findOrFail($id));

        $show->id('Id');
        $show->first_id('直推用户');
        $show->second_id('分推用户');
        $show->user_id('关联用户');
        $show->project_id('推广项目');
        $show->created_at('创建时间');
        $show->updated_at('更新时间');
        $show->status('推广状态');
        $show->effective_time('切换有效的时间');
        $show->follow_time('继续跟进中的时间');
        $show->transaction_time('成交时间');
        $show->effective_first_reward('有效推广奖励');
        $show->effective_share_reward('有效推广朋友圈奖励');
        $show->follow_first_reward('继续跟进推广奖励');
        $show->follow_share_reward('跟进分享朋友圈奖励');
        $show->transaction_first_reward('成交推广奖励');
        $show->transaction_share_reward('成交分享朋友圈奖励');
        $show->second_reward('上级推广分成 ');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new BrokerCustomer);
        $form->tab('基本信息',function(Form $form){
            $form->text('title', '名称');
            $form->ckeditor('desc', '描述');
        })->tab('分享规则',function(Form $form) {
            $form->number('first_id', '直推用户');
            $form->number('second_id', '分推用户');
            $form->number('user_id', '关联用户');
            $form->number('project_id', '推广项目');
            $form->text('status', '推广状态');
            $form->datetime('effective_time', '切换有效的时间')->default(date('Y-m-d H:i:s'));
            $form->datetime('follow_time', '继续跟进中的时间')->default(date('Y-m-d H:i:s'));
            $form->datetime('transaction_time', '成交时间')->default(date('Y-m-d H:i:s'));
            $form->currency('effective_first_reward', '有效客户奖励直推奖励')->symbol('元');
            $form->currency('effective_share_reward', '有效分享奖励')->symbol('元');
            $form->currency('follow_first_reward', '跟进直推奖励')->symbol('元');
            $form->currency('follow_share_reward', '跟进分享奖励')->symbol('元');
            $form->currency('transaction_first_reward', '成交直推奖励')->symbol('元');
            $form->currency('transaction_share_reward', '成交分享奖励')->symbol('元');
            $form->currency('second_reward', '直推上级奖励')->symbol('%');
        });

        return $form;
    }
}
